<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class Banner
 *
 * @package App
 * @mixin \Eloquent
 */
class Zxbanner extends Model
{
    public $timestamps = false;
    protected $table = 'kl_banner';


    //跳转到
    const TYPE_NONE = 2;
    const TYPE_WEB = 1; 

    static $TypeDisplay = [
        self::TYPE_NONE => '不跳转',
        self::TYPE_WEB => '跳转'
    ];

    //内部跳转位置
    const ZXTYPE_POSTINGS = 1;
    const ZXTYPE_SHOP = 2;
    const ZXTYPE_SHANG = 3;	
    const ZXTYPE_GAME = 4;
	//const ZXTYPE_AD = 5;
	const ZXTYPE_DK = 5;
    static $ZxtypeDisplay = [
        self::ZXTYPE_POSTINGS => 'web',
        self::ZXTYPE_SHOP => '帖子',
        self::ZXTYPE_SHANG => '商家',
        self::ZXTYPE_GAME => '游戏乐园',
		//self::ZXTYPE_AD => '广告',		
		self::ZXTYPE_DK => '晨起打卡',
    ];

    const JUMP_YES = 1; 
    const JUMP_NO = 2;

    /**
     * 添加banner
     * @param $request
     * @return array
     */
    static function BannerPublish($request)
    {
        $banner = new Zxbanner();
        //banner
        if ($request->has('purl')) {
            $path = Storage::url(Postings::OssUpdate($request->purl));
            $banner->purl = $path;
        } else {
            return ['status' => 2, 'message' => '图片呢?'];
        }
        if (intval($request->is_jump) === self::TYPE_NONE) {
            $banner->is_jump = self::JUMP_NO;
        } else {
            $banner->link = $request->link;
        }
		/*if(intval($request->type) === self::ZXTYPE_SHOP){
			if (!is_numeric($request->link_id)) {
				return ['status' => 2, 'message' => 'ID是数字哦'];
			}
			$banner->link_id = $request->link_id;
		}*/
        $banner->sort = $request->sort;
        $banner->type = $request->type;
        if (!$banner->save()) {
            return ['status' => 2, 'message' => '添加失败'];
        }

        return ['status' => 1, 'message' => '添加成功'];
    }


    /**
     * 修改banner
     * @param $request
     * @param $banner
     * @return array
     */
    static function BannerModify($request, $banner)
    {

        //banner
        if ($request->has('purl')) {
            $path = Storage::url(Postings::OssUpdate($request->purl));
            $banner->purl = $path;
        }

        if (intval($request->is_jump) === self::TYPE_NONE) {
            $banner->is_jump = self::JUMP_NO;
        } else {
            $banner->is_jump = self::JUMP_YES;
            $banner->link = $request->link;
        }

		/*if(intval($request->type) === self::ZXTYPE_SHOP){
			if (!is_numeric($request->link_id)) {
				return ['status' => 2, 'message' => 'ID是数字哦'];
			}
			$banner->link_id = $request->link_id;
		}*/
        $banner->sort = $request->sort;
        $banner->type = $request->type;
        if (!$banner->save()) {
            return ['status' => 2, 'message' => '修改失败'];
        }

        return ['status' => 1, 'message' => '修改成功'];
    }

}
